import OSS from 'ali-oss';
import { Buffer } from 'buffer';

const client = new OSS({
  region: '', // 例如：'oss-cn-hangzhou'
  accessKeyId: 'LTAI5t8NyLfXW1xzHpQASvuE',
  accessKeySecret: 'yHeQGSSfhFMCKMdXNp1kJgRFY4GsAP',
  bucket: "https://chanmotend.oss-cn-beijing.aliyuncs.com/"
});

export async function uploadBase64ToOSS(base64, objectName) {
  const base64Data = base64.replace(/^data:image\/\w+;base64,/, "");
  const buffer = Buffer.from(base64Data, 'base64');

  try {
    const result = await client.put(objectName, buffer);
    console.log(result); // 返回的信息中包含访问URL
    return result.url;
  } catch (e) {
    console.error(e);
  }
}
